package com.xjscrm.console.chat.utils;

import io.netty.channel.Channel;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;

/**
 * @author mingyang.chen create in 2023/4/23 16:01
 */
public class ChannelUtils {

    public static final AttributeKey<Long> staffIdKey = AttributeKey.newInstance("staff");
    public static final AttributeKey<String> closeDescAttrKey = AttributeKey.newInstance("closeDesc");

    public static synchronized void cleanStaff(Channel channel) {
        setStaff(channel, null);
    }

    public static synchronized boolean setStaff(Channel channel, Long staffId) {
        Attribute<Long> attr = channel.attr(staffIdKey);
        return attr.compareAndSet(attr.get(), staffId);
    }

    public static synchronized void setCloseAttr(Channel channel, String closeDesc) {
        Attribute<String> attr = channel.attr(closeDescAttrKey);
        attr.compareAndSet(attr.get(), closeDesc);
    }

    public static synchronized Long getStaff(Channel channel) {
        Attribute<Long> attr = channel.attr(staffIdKey);
        return attr.get();
    }

    public static synchronized String getCloseAttr(Channel channel) {
        Attribute<String> attr = channel.attr(closeDescAttrKey);
        return attr.get();
    }

    public static String getChannelId(Channel channel) {
        return channel.id().asShortText();
    }
}
